"""This slot is used to filter invalid words or chars before analyzing the question.

Function 'handle' must be implemented which will be called in 'Robot'.
"""

import re


class FilterSlot:

    def handle(self, question):
        """Filter invalid words or chars in the question.

        Example:
            question: '台风的英文名是什么？(☆▽☆)'
            filter_question: '台风的英文名是什么？'
        
        Args:
            question (str): question from user.
        
        Returns:
            str: Filtered question.
        """
        return self.filter_str(question)
    
    def filter_str(self, desstr,restr=''):
        """Filter chars except for English and Chinese chars, numbers.
        
        Args:
            desstr (str): String to be filtered.
            restr (str, optional): Every matched sub-string will be replaced with restr. Defaults to ''.

        Returns:
            Filterd string.
        """
        res = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]")
        return res.sub(restr, desstr)
